package com.smz.controller;

import com.smz.entity.PageEntity;
import com.smz.entity.TProductOrder;
import com.smz.service.ProductOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
public class ProductOrderController {

    @Autowired
    private ProductOrderService productOrderService;

    @ApiOperation("这是查询所有订单的接口")
    @GetMapping("/orders")
    public ResponseEntity<List<TProductOrder>> findAll(){
        List<TProductOrder> orders = productOrderService.findAll();
        return ResponseEntity.ok(orders);
    }

    @ApiOperation("分页查询")
    @GetMapping("/orders/page/{current}")
    public ResponseEntity<PageEntity<TProductOrder>> findOrderPage(@PathVariable("current") Integer current){
        System.out.println(current);
        PageEntity<TProductOrder> page = productOrderService.findOrderPage(current);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("添加订单")
    @PostMapping("order/add")
    public ResponseEntity<String>addOrder(@RequestBody TProductOrder productOrder){
        productOrderService.addOrder(productOrder);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("修改订单状态")
    @PutMapping("order/{id}/{orderStatus}")
    public ResponseEntity<String>modifyOrderById(@PathVariable("id")Integer id, @PathVariable("orderStatus")String orderStatus) {
        productOrderService.modifyOrderById(id,orderStatus);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("根据时间或订单号查")
    @GetMapping("order/{id}/{dealTime}")
    public ResponseEntity<List<TProductOrder>> findByIdTime(@PathVariable("id") String id,@PathVariable("dealTime") String dealTime){
        List<TProductOrder> order=null;
        if (id.equals("null")){
            if (dealTime.equals("null")){

            }else {
                order = productOrderService.findOrderByTime(dealTime);
            }

        }else {
            if (dealTime.equals("null")){
                order = productOrderService.findOrderByOrderId(Integer.valueOf(id));
            }else {
                order = productOrderService.findOrderByIdTime(Integer.valueOf(id), dealTime);
            }

        }
        return ResponseEntity.ok(order);
    }

    @ApiOperation("根据排单查询")
    @GetMapping("order/{productStatus}")
    public ResponseEntity<List<TProductOrder>> findProductStatus(@PathVariable("productStatus") String  productStatus){
        List<TProductOrder> tProductOrders = productOrderService.findProductStatus(productStatus);
        return ResponseEntity.ok(tProductOrders);
    }

    @ApiOperation("设置排单")
    @PutMapping("/order")
    public ResponseEntity <String> modifyStatusById(@RequestBody TProductOrder tProductOrder){
        productOrderService.modifyStatusById(tProductOrder);
        return ResponseEntity.ok("ok");
    }

}
